package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.product.CategoryInfo;
import com.zuipin.mapper.CategoryInfoMapper;
import com.zuipin.service.ICategoryInfoService;
import com.zuipin.vo.CategoryInfoVo;

import net.sf.json.JSONObject;

/**
 * 品类信息业务类实现
 *
 * @ClassName CategoryInfoServiceImpl 
 * @author wangcongyi
 * @date 2017年4月18日 下午5:35:02
 */
@Service
public class CategoryInfoServiceImpl implements ICategoryInfoService {
	
	@Resource
	private CategoryInfoMapper categoryInfoMapper;
	
	@Override
	public void deleteByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void insert(CategoryInfo record) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void insertSelective(CategoryInfo record) {
		// TODO Auto-generated method stub
		categoryInfoMapper.insertSelective(record);
	}
	
	@Override
	public CategoryInfo selectByPrimaryKey(Long id) {
		return categoryInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void updateByPrimaryKeySelective(CategoryInfo record) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void updateByPrimaryKey(CategoryInfo record) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<CategoryInfoVo> findRootCategoryInfo(Long sysId) {
		// TODO Auto-generated method stub
		List<CategoryInfoVo> vos = new ArrayList<CategoryInfoVo>();
		// 查找根路径品类列表
		List<CategoryInfo> units = categoryInfoMapper.findRootCategoryInfo(sysId);
		JSONObject params = new JSONObject();
		params.put("sysId", sysId);
		// 循环查找子路径品类列表
		for (CategoryInfo unit : units) {
			CategoryInfoVo vo = new CategoryInfoVo();
			vo.setId(unit.getId());
			vo.setParentId(unit.getParentId());
			vo.setCatName(unit.getCatName());
			vo.setCatSeq(unit.getCatSeq());
			params.put("parentId", unit.getId());
			vo.setChildren(findCategoryInfoTree(params));
			vo.setIcoUrl(unit.getIcoUrl());
			vos.add(vo);
		}
		return vos;
	}
	
	@Override
	public List<CategoryInfoVo> findCategoryInfoTree(JSONObject obj) {
		// TODO Auto-generated method stub
		List<CategoryInfoVo> vos = new ArrayList<CategoryInfoVo>();
		List<CategoryInfo> units = listCategoryInfoByParent(obj);
		for (CategoryInfo unit : units) {
			CategoryInfoVo vo = new CategoryInfoVo();
			vo.setId(unit.getId());
			vo.setParentId(unit.getParentId());
			vo.setCatName(unit.getCatName());
			vo.setCatSeq(unit.getCatSeq());
			obj.put("parentId", unit.getId());
			vo.setChildren(findCategoryInfoTree(obj));
			vo.setIcoUrl(unit.getIcoUrl());
			vos.add(vo);
		}
		return vos;
	}
	
	@Override
	public List<CategoryInfo> listCategoryInfoByParent(JSONObject obj) {
		// TODO Auto-generated method stub
		List<CategoryInfo> categoryInfos = categoryInfoMapper.listCategoryInfoByParent(obj);
		return categoryInfos;
	}
	
	@Override
	public CategoryInfo selectBySysId(JSONObject obj) {
		// TODO Auto-generated method stub
		return categoryInfoMapper.selectBySysId(obj);
	}
	
	@Override
	public List<CategoryInfo> findCategoryNav(Long catId) {
		// TODO Auto-generated method stub
		List<CategoryInfo> categoryInfos = new ArrayList<>();
		CategoryInfo subcategoryInfo = categoryInfoMapper.findSubCategoryInfo(catId);
		if (null != subcategoryInfo && null != subcategoryInfo.getParentId() && CategoryInfo.ROOT != subcategoryInfo.getParentId()) {
			categoryInfos.add(subcategoryInfo);
			// 判断是否有父级品类
			if (null != subcategoryInfo.getParentId() && CategoryInfo.ROOT != subcategoryInfo.getParentId()) {
				CategoryInfo categoryInfo = categoryInfoMapper.fingParentCategoryInfo(subcategoryInfo.getParentId());
				if (null != categoryInfo && null != categoryInfo.getParentId() && CategoryInfo.ROOT != categoryInfo.getParentId()) {
					categoryInfos.add(categoryInfo);
					// if( null!=categoryInfo.getParentId()&& CategoryInfo.ROOT!=categoryInfo.getParentId()){
					// CategoryInfo parentCategoryInfo=categoryInfoMapper.fingParentCategoryInfo(categoryInfo.getParentId());
					// if(parentCategoryInfo !=null && null!=parentCategoryInfo.getParentId() && CategoryInfo.ROOT!=parentCategoryInfo.getParentId() ){
					// categoryInfos.add(parentCategoryInfo);
					// }
					// }
				}
			}
		}
		Collections.reverse(categoryInfos);
		return categoryInfos;
	}
	
}
